package ar.com.android.restoapp.providers;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import ar.com.android.restoapp.dbHelpers.SQLFoodOrderHelper;
import ar.com.android.restoapp.entities.OrderItems;

public class OrderItemDataSource {
	private static final String TAG = "OrderItemDataSource";
	
	private static final String DB_COLUMN_0_NAME = "idOrderFoodItem";
	private static final String DB_COLUMN_1_NAME = "idTable";
	private static final String DB_COLUMN_2_NAME = "idFood";
	private static final String DB_COLUMN_3_NAME = "description";
	private static final String DB_COLUMN_4_NAME = "quantity";
	private static final String DB_COLUMN_5_NAME = "price";
	private static final String DB_COLUMN_6_NAME = "state";
	
	private SQLiteDatabase database;
	private SQLFoodOrderHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME, DB_COLUMN_2_NAME, DB_COLUMN_3_NAME, DB_COLUMN_4_NAME, DB_COLUMN_5_NAME, DB_COLUMN_6_NAME};
	
	public OrderItemDataSource(Context context) {
		dbHelper = SQLFoodOrderHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
		
	public long insertFood(OrderItems orderItem)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, orderItem.getIdTable());
		contentValues.put(DB_COLUMN_2_NAME, orderItem.getIdFood());
		contentValues.put(DB_COLUMN_3_NAME, orderItem.getOrderDescription());
		contentValues.put(DB_COLUMN_4_NAME, orderItem.getOrderQuantity());
		contentValues.put(DB_COLUMN_5_NAME, orderItem.getPrice());
		contentValues.put(DB_COLUMN_6_NAME, orderItem.getState());
		
		long insertId = database.insert(SQLFoodOrderHelper.DB_ORDERCHILD_TABLE_NAME, null, contentValues);
		return insertId;
	}
	
	public boolean removeOrderItem(Integer idOrderItem)
	{
		int result = database.delete(SQLFoodOrderHelper.DB_ORDERCHILD_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idOrderItem, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeOrderItem()
	{
		int result = database.delete(SQLFoodOrderHelper.DB_ORDERCHILD_TABLE_NAME, null, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeTableOrderItem(Integer idTable)
	{
		int result = database.delete(SQLFoodOrderHelper.DB_ORDERCHILD_TABLE_NAME, DB_COLUMN_1_NAME + " = " + idTable, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public long updateOrderItem(OrderItems orderItem)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, orderItem.getIdTable());
		contentValues.put(DB_COLUMN_2_NAME, orderItem.getIdFood());
		contentValues.put(DB_COLUMN_3_NAME, orderItem.getOrderDescription());
		contentValues.put(DB_COLUMN_4_NAME, orderItem.getOrderQuantity());
		contentValues.put(DB_COLUMN_5_NAME, orderItem.getPrice());
		contentValues.put(DB_COLUMN_6_NAME, orderItem.getState());
		
		return database.update(SQLFoodOrderHelper.DB_ORDERCHILD_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + orderItem.getOrderItemId(), null);
	}
	
	public List<OrderItems> getAllOrderItems(Integer idTable) 
	{
		Cursor cursor = null;
		List<OrderItems> orders = new ArrayList<OrderItems>();
		
		try
		{
			//Cursor cursor = database.query(SQLFoodOrderHelper.DB_ORDERCHILD_TABLE_NAME, allColumns, DB_COLUMN_1_NAME + " = " + idTable + " AND " + DB_COLUMN_2_NAME + " = " + idOrderFood, null, null, null, null);
			cursor = database.query(SQLFoodOrderHelper.DB_ORDERCHILD_TABLE_NAME, allColumns, DB_COLUMN_1_NAME + " = " + idTable, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
				OrderItems order = new OrderItems();
				order.setOrderItemId(cursor.getInt(0));
				order.setIdTable(cursor.getInt(1));
				order.setIdFood(cursor.getInt(2));
				order.setOrderDescription(cursor.getString(3));
				order.setOrderQuantity(cursor.getDouble(4));
				order.setPrice(cursor.getDouble(5));
				order.setSelected(false);
				order.setState(cursor.getString(6));
				
				orders.add(order);
				cursor.moveToNext();
			}
			return orders;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return orders;
		}
		finally {
			cursor.close();
		}
	}
}
